//
//  WelcomeViewController.swift
//  weibo_swift
//
//  Created by dulinlong on 15/6/30.
//  Copyright © 2015年 sheji_56. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {
    /// 头像的y约束,用于动画
    var iconLayoutY:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //如果有用户登录信息,加载头像
        if let urlString = sharedUserAccount?.avatar_large{
            iconImageView.sd_setImageWithURL(NSURL(string:urlString))
        }

    }
    
    override func loadView() {
        view = UIView()
        
        //添加控件
        view.addSubview(backImageView)
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        //设置布局
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        iconLayoutY = NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
        view.addConstraint(iconLayoutY!)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view1(85)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics:
            nil, views: ["view1":iconImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1(85)]"
            , options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view1":iconImageView]))
        
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view1]-20-[view2]", options: NSLayoutFormatOptions.AlignAllCenterX, metrics: nil, views: ["view1":iconImageView,"view2":welcomeLabel]))
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconLayoutY?.constant = -UIScreen.mainScreen().bounds.height * 0.5 + 100

        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { _ in
            self.view.layoutIfNeeded()
            
            }) { _ in
                
                UIView.animateWithDuration(1.5, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootControllerNotification, object: true)
                })
        }
    }
    
    /// 背景图片
    lazy var backImageView:UIImageView = {
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        backImageView.contentMode = UIViewContentMode.ScaleToFill
        backImageView.frame = UIScreen.mainScreen().bounds
        return backImageView
    }()
  
    /// 头像
    lazy var iconImageView:UIImageView = {
    
       let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 43
        icon.layer.masksToBounds = true
        
        return icon
    }()
    
    ///  欢迎文字
    lazy var welcomeLabel: UILabel = {
       let label = UILabel()
        label.font = UIFont.systemFontOfSize(14)
        label.text = "欢迎回来"
        label.textColor = UIColor.blackColor()
        label.sizeToFit()
        label.alpha = 0.0
        return label
    }()
}
